﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Vibz.Common.Channel;
using Vibz.Server.Controller.UI;
using Vibz.Common;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.Threading;
using System.IO.Pipes;
using System.IO;
using Vibz.Common.Model;

namespace Vibz.Terminal
{
    public partial class FormHome : Form
    {
        XmlModelSource xModelSource;
        public FormHome(Logs log)
        {
            InitializeComponent();

            DataGridViewImageColumn AcceptColumn = new DataGridViewImageColumn();
            AcceptColumn.Icon = Vibz.Terminal.Properties.Resources.connect;
            AcceptColumn.ImageLayout = DataGridViewImageCellLayout.Normal;
            AcceptColumn.ValuesAreIcons = true;
            AcceptColumn.Name = "Connect";
            AcceptColumn.CellTemplate.Style.BackColor = Color.Honeydew;
            dgvModelView.Columns.Add(AcceptColumn);

            DataGridViewImageColumn StartColumn = new DataGridViewImageColumn();
            StartColumn.Icon = Vibz.Terminal.Properties.Resources.terminal;
            StartColumn.ImageLayout = DataGridViewImageCellLayout.NotSet;
            StartColumn.ValuesAreIcons = true;
            StartColumn.Name = "Standalone";
            StartColumn.CellTemplate.Style.BackColor = Color.Honeydew;
            dgvModelView.Columns.Add(StartColumn);

            

            LoadModels();
        }

        void LoadModels()
        {
            try
            {
                dgvModelView.Rows.Clear();
                xModelSource = new XmlModelSource(TerminalProcessor.TerminalDataSource);
                dgvModelView.DataSource = xModelSource.Models;
                dgvModelView.Columns["AllowSingleUser"].Visible = false;
                dgvModelView.Columns["AllowMultiUser"].Visible = false;
                dgvModelView.Columns["Type"].Visible = false;
                dgvModelView.Columns["Path"].Visible = false;
                dgvModelView.Columns["Client"].Visible = false;

            }
            catch (Exception exc)
            {
                lstMessage.Items.Add("Could not load the configuration file." + exc.Message);
            }
        }

        void RunClientTerminalInIsolation(ModelInformation mInfo)
        {
            ConnectToRunningServer(mInfo, TerminalProcessor.RunServer(mInfo));
        }

        void ConnectToRunningServer(ModelInformation mInfo, string sLocation)
        {
            ((FormContainer)this.MdiParent).OpenForm(FormType.ClientTerminal, new object[] { mInfo, new SocketClient(sLocation) });
        }

        private void dgvModelView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvModelView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
                return;
            string command = dgvModelView.Columns[e.ColumnIndex].Name.ToString();
            ModelInformation mView = (ModelInformation)dgvModelView.Rows[e.RowIndex].DataBoundItem;
            switch (command.ToLower())
            {
                case "connect":
                    FormConnect fConnect = new FormConnect();
                    if (fConnect.ShowDialog() == DialogResult.OK)
                    {
                        ConnectToRunningServer(mView, fConnect.ServerLocation);
                    }
                    break;
                case "standalone":
                    RunClientTerminalInIsolation(mView);
                    break;
            }
        }
        
    }
}
